#pragma once
#include "cgzITask.h"

class cgzCTcpC;

enum ETCP_C
{
    TCPC_LOOP,
    TCPC_ACPT,
    TCPC_SEND,
    TCPC_RECV,
    TCPC_DISC,
    TCPC_GADD,
    TCPC_GDEL,
    TCPC_GCLR,
};

class cgzCLoopC : public cgzIData
{
public:
    friend class cgzCTcpC;
    cgzCLoopC(ETCP_C etype);
    virtual ~cgzCLoopC();

public:
    ETCP_C getEType() const;

public:
    ETCP_C type;
};

class cgzCAcptC : public cgzCLoopC
{
public:
    friend class cgzCTcpC;
    cgzCAcptC();
    virtual ~cgzCAcptC();

private:
    ETCP_C type;
};

class cgzCSendC : public cgzCLoopC
{
public:
    friend class cgzCTcpC;
    cgzCSendC();
    // 普通发送
    cgzCSendC(
        const int mid, 
        const char *data, 
        const int len, 
        const long cid);
    // 发给服务
    cgzCSendC(cgzHeader &header, const int ufd);
    virtual ~cgzCSendC();

public: 
    ETCP_C type;
    char *m_data;
    int m_len;
    int m_sendlen;
};

class cgzCRecvC : public cgzCLoopC
{
public:
    friend class cgzCTcpC;
    cgzCRecvC(int orgid, int tarid, const char *data, const int len);
    cgzCRecvC(int orgid, int tarid, const int len);
    virtual ~cgzCRecvC();

private:
    ETCP_C type;

public:
    char *m_data;
    int m_len;
};

class cgzCDiscC : public cgzCLoopC
{
public:
    friend class cgzCTcpC;
    cgzCDiscC(int orgid, int tarid);
    virtual ~cgzCDiscC();

private:
};

class cgzCGAddC : public cgzCLoopC
{
public:
    friend class cgzCTcpC;
    cgzCGAddC();
    virtual ~cgzCGAddC();

public:
    std::unordered_map<int, int> map;
};

class cgzCGDelC : public cgzCLoopC
{
public:
    friend class cgzCTcpC;
    cgzCGDelC();
    virtual ~cgzCGDelC();

public:
    std::unordered_set<int> set;
};

class cgzCGClrC : public cgzCLoopC
{
public:
    friend class cgzCTcpC;
    cgzCGClrC();
    virtual ~cgzCGClrC();
};

class cgzCTcpC : public cgzITask
{
public:
    cgzCTcpC(
        cgzCThreadPool *pool, 
        const char *name, 
        const std::string ip, 
        const int port, 
        const int cid);
    virtual ~cgzCTcpC();

public:
    bool run(int64_t nms) override;
    virtual bool run(cgzCLoopC *task);
    bool casBusy() override;
    bool retTask(cgzIData *cgzTask) override;

public:
    virtual bool tcpinit();

protected:
    virtual bool tcplink();
    virtual bool tcpread();
    virtual bool tcpsend();
    virtual bool disctcp();
    virtual bool todoret(cgzIData *dtask);

public:
    virtual bool connect(bool is_hand = true);
    virtual bool handshake();
    virtual bool sendaccept();
    virtual bool sticking();

protected:
    std::string m_ip;
    int m_port;
    int m_cid;
    int m_fd;
    bool m_hand;

protected:
    cgzCireBuffer m_readCire;
    cgzCireBuffer m_sendCire;
};
